package tech.yunland.shop.web;

import com.moyuer.bsf.common.Pagination;
import com.moyuer.bsf.web.controller.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tech.yunland.shop.domain.model.SaleBillEntry;
import tech.yunland.shop.domain.model.report.Commission;
import tech.yunland.shop.domain.service.report.CommissionService;

import java.text.ParseException;
import java.util.List;

/**
 * 提成日报控制器
 *
 * @author 王睿智 <geekrize@gmail.com>
 *
 */
@RestController
@RequestMapping("/commission")
public class CommissionController extends BaseController {

	@Autowired
	private CommissionService commissionService;

	/**
	 * 提成月报
	 *
	 * @param bizDate
	 * @return Map<String, Object>
	 * @throws ParseException
	 */
	@RequestMapping(value = "/month", method = RequestMethod.GET)
	public Pagination<Commission> getResultMonInfo(@RequestParam String bizDate,
			@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer rows)
			throws ParseException {

		Pagination<Commission> result = new Pagination<Commission>(page, rows);

		List<Commission> records = commissionService.getMonthCommission(bizDate, primaryOrganId);

		result.setCount(records.size());
		result.setRecords(records);

		return result;
	}

	/**
	 * 提成详情
	 * 
	 * @param bizDate
	 * @param type
	 *            月报/日报（month/day）
	 * @param userId
	 * @param page
	 * @param rows
	 * @return 
	 * @throws ParseException
	 */
	@RequestMapping(value = "/entry", method = RequestMethod.GET)
	public Pagination<SaleBillEntry> getResultEntryInfo(
			@RequestParam String bizDate, 
			@RequestParam String type,
			@RequestParam String userId, 
			@RequestParam(required = false) Integer page,
			@RequestParam(required = false) Integer rows) throws ParseException {

		Pagination<SaleBillEntry> result = new Pagination<SaleBillEntry>(page, rows);
		int startIdx = result.getStartIdx();
		rows = result.getRows();
		List<SaleBillEntry> records = commissionService.getCommissionEntry(type, bizDate, primaryOrganId, userId,startIdx,rows);
		int recordsSize = commissionService.getCommissionEntrySize(type, bizDate, primaryOrganId, userId);

		result.setCount(recordsSize);
		result.setRecords(records);

		return result;
	}

	/**
	 * 提成日报
	 *
	 * @param organId
	 * @param userId
	 * @param startIndex
	 * @param length
	 * @return Income
	 * @throws ParseException
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public Pagination<Commission> getResultDayInfo(@RequestParam(required = false) String bizDate,
			@RequestParam(required = false) String userId, @RequestParam(required = false) Integer page,
			@RequestParam(required = false) Integer rows) throws ParseException {

		Pagination<Commission> result = new Pagination<Commission>(page, rows);
		int startIdx = result.getStartIdx();

		int count = commissionService.getResultSize(bizDate, userId, this.primaryOrganId);
		List<Commission> records = commissionService.getResultInfo(bizDate, userId, this.primaryOrganId, startIdx,
				rows);

		result.setCount(count);
		result.setRecords(records);

		return result;
	}

}
